package com.pearlsoft.webapp.sysmgr.param;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;

import com.pearlsoft.commons.utils.StringUtil;
import com.pearlsoft.commons.utils.UUIDUtil;
import com.pearlsoft.webapp.core.WebException;
import com.pearlsoft.webapp.core.dataset.DataSet;
import com.pearlsoft.webapp.core.dataset.Record;
import com.pearlsoft.webapp.core.dataset.Result;
import com.pearlsoft.webapp.core.dataset.spi.ContextDataSetHandler;

public class SysParamDataSetHandler extends ContextDataSetHandler {

	public void handler(DataSet ds, Result result) throws Exception {
		// TODO Auto-generated method stub

	}
	public void save(final DataSet ds, Result result) throws Exception {
		JdbcTemplate jdbcTemplate = this.getBean("jdbcTemplate", JdbcTemplate.class);
		while(ds.next()){
			Record record = ds.getCurrentRecord();
			String paramId =(String)record.getValue("PARAM_ID");
			String paramKey = (String)record.getValue("PARAM_KEY");
			boolean isCodeDup = this.checkCodeDup(paramId,paramKey );
			if( isCodeDup ){
				throw new WebException("用户编码已经存在");
			}
			if(StringUtil.isEmpty(paramId)){
				this.insert(record, result,jdbcTemplate);
			}else{
				this.update(record, result,jdbcTemplate);
			}
		}
		
	}
	
	public void insert(final Record record, Result result,JdbcTemplate jdbcTemplate) throws Exception {
		String sql = "insert into SYS_PARAM(PARAM_ID,PARAM_NAME,PARAM_KEY,PARAM_VALUE,PARAMS_TYPE,C_STATUS,C_REMARK) values(?,?,?,?,?,?,?)";
		jdbcTemplate.update(sql, new PreparedStatementSetter() {
			public void setValues(PreparedStatement ps) throws SQLException {
				ps.setString(1, UUIDUtil.get());
				ps.setString(2, (String) record.getValue("PARAM_NAME"));
				ps.setString(3, (String) record.getValue("PARAM_KEY"));
				ps.setString(4, (String) record.getValue("PARAM_VALUE"));
				ps.setString(5, (String) record.getValue("PARAMS_TYPE"));
				ps.setString(6, (String) record.getValue("C_STATUS"));
				ps.setString(7, (String) record.getValue("C_REMARK"));
			}

		});
	}
	
	public void update(final Record record, Result result,JdbcTemplate jdbcTemplate) throws Exception {
		String sql = "update SYS_PARAM set PARAM_NAME=?,PARAM_KEY=?,PARAM_VALUE=?,PARAMS_TYPE=?,C_STATUS=?,C_REMARK=? where PARAM_ID=?";
		jdbcTemplate.update(sql, new PreparedStatementSetter() {
			public void setValues(PreparedStatement ps) throws SQLException {
				ps.setObject(1, record.getValue("PARAM_NAME"));
				ps.setObject(2, record.getValue("PARAM_KEY"));
				ps.setObject(3, record.getValue("PARAM_VALUE"));
				ps.setObject(4, record.getValue("PARAMS_TYPE"));
				ps.setObject(5, record.getValue("C_STATUS"));
				ps.setObject(6, record.getValue("C_REMARK"));
				ps.setObject(7, record.getValue("PARAM_ID"));
			}
		});

	}
	public void delete(final DataSet ds, Result result) throws Exception {
		JdbcTemplate jdbcTemplate = this.getBean("jdbcTemplate", JdbcTemplate.class);
		String sql = "delete from SYS_PARAM where PARAM_ID=?";
		jdbcTemplate.update(sql, new PreparedStatementSetter() {
			public void setValues(PreparedStatement ps) throws SQLException {
				ps.setString(1, (String) ds.getConditions().get("PARAM_ID"));
			}
		});
	}
	
	public boolean checkCodeDup(String paramId,String paramKey){
		JdbcTemplate jdbcTemplate = this.getBean("jdbcTemplate", JdbcTemplate.class);
		String sql ="";
		Map<String,String> params = new HashMap<String,String>();
		Object[] objects =null;
		if( StringUtils.isBlank(paramId)){
			sql =" select count(*) from SYS_PARAM where PARAM_KEY =?";
			objects = new Object[]{paramKey};
			params.put("user_code", paramKey);
		}else{
			sql =" select count(*) from SYS_PARAM where PARAM_KEY =? and PARAM_ID <>?";
			objects = new Object[]{paramKey,paramId};
		}
		int count = jdbcTemplate.queryForInt(sql, objects);
		return count > 0 ? true:false;
		
	}
}
